/*
* @Author: zlx
* @Date:   2018-03-03 20:08:24
* @Last Modified by:   zlx
* @Last Modified time: 2018-03-03 21:00:03
*/
var time=60;
$(function(){
	$('.back').on('click',function(){
		window.history.go(-1);
	})
	var panter=/^1[34578][0-9]{9}$/;
	var user_info=localStorage.getItem('user_info');
		user_info=JSON.parse(user_info);
	//获取验证码
	$(document).on('click','#send',function(){
		if(''==$('.phone_num').val()){
			tips('请填写手机号');
			return false;
		}else if(! panter.test( $('.phone_num').val() )){
			tips('手机号码格式不正确');
			return false;
		}else{
    		$.post(apicom+'mobile/view/member/checkOpenId',{
    			phone:$('.phone_num').val()
    		},function(res){
    			if(typeof res == "string"){
		            res=JSON.parse(res);
		        }
    			if(1==res.code){
    				if(1==res.content.bindStatus){
    					tips('该手机号已经绑定qq')
    					$('.bac').css({'display':'block'})
    					$('#qq b').html(res.content.qq_nick_name);
    					$('#qq').css({'display':'flex'});
    				}
    				if(2==res.content.bindStatus){
    					tips('该手机号已经绑定微信')
    					$('.bac').css({'display':'block'})
    					$('#wx b').html(res.content.wx_nick_name);
    					$('#wx').css({'display':'flex'});
    				}
    				if(2==res.content.bindStatus){
    					tips('该手机号已经绑定微信和qq')
    					$('.bac').css({'display':'block'})
    					$('#qq b').html(res.content.qq_nick_name);
    					$('#wx b').html(res.content.wx_nick_name);
    					$('#wx').css({'display':'flex'});
    					$('#qq').css({'display':'flex'});
    				}
    				$('.que').on('click',function(){
    					$('.bac').css({'display':'none'});
    					tips('请更换手机号');
    					$('.phone_num').val('');
    				})
    				if(0==res.content.bindStatus){
    					$.post(apicom+'mobile/view/member/sendSms',{
							phone:$('.phone_num').val(),
							type:4//4三方绑定注册
						},function(res){
							if(typeof res == "string"){
					            res=JSON.parse(res);
					        }
							setTimeout(function(){
								tips(res.msg)
							},1000);
							$('.send').attr('id','');
				            $('.send').text("重新获取" + "(" + (time) + ")");
				            var setId = setInterval(function() {
				                if (time <= 0) {
				                    $('.send').attr('id','send');
				                    $('.send').text("发送验证码");
				                    clearInterval(setId);
				                    time=60;
				                } else {
				                    time--;
				                    $('.send').text("重新获取" + "(" + (time) + ")");
				                }
				            }, 1000);
						})
    				}
    			}
			})
		}
	})
	//绑定
	$(document).on('click','.bangding',function(){
		if(''==$('.phone_num').val()){
			tips('请填写手机号');
			return false;
		}else if(! panter.test( $('.phone_num').val() )){
			tips('手机号码格式不正确');
			return false;
		}else if(''==$('.user_code').val()){
			tips('请输入验证码');
			return false;
		}else{
			user_info.phone=$('.phone_num').val();
			user_info.smsCode=$('.user_code').val();
			san_login(user_info);
		}
	})
	
})
//三方授权绑定手机号
function san_login(user_info){
	$.post(apicom+'mobile/view/member/thirdParty-login',user_info,function(res){
//	$.post('http://192.168.30.226:10001/mobile/view/member/thirdParty-login',user_info,function(res){
		if(typeof res == "string"){
            res=JSON.parse(res);
        }
    	if(1==res.code){
    		tips('绑定成功');
    		user_info.nickname=res.nickname;
    		user_info.uuid=res.uuid;
    		user_info.token=res.token;
    		user_info.id=res.id;
    		localStorage.setItem('user_info',JSON.stringify(user_info));
    		setTimeout(function(){
    			window.location.href="../homePage/sy.html";
    		},1000)
    	}
    })
}

